package com.northone.statistics.fragment;

import com.northone.statistics.BaseFragment;
import com.northone.statistics.R;
import com.northone.statistics.activity.MonthDataActivity;
import com.northone.statistics.adapter.LeftMonthAdapter;
import com.northone.statistics.database.SaintDataBaseHelper;
import com.northone.statistics.utils.Constans;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class StatisFragment extends BaseFragment {
	private View view;
	public static final int REQUEST_CODE_MONTH = 3744;
	private SimpleCursorAdapter adapter;
	private Cursor cursor;

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_statis, null);

		initTitleBar();
		initView();
		return view;
	}

	private void initTitleBar() {
		view.findViewById(R.id.iv_lefticon).setVisibility(View.GONE);
		TextView title = (TextView) view.findViewById(R.id.tv_title);
		title.setText(R.string.statis);
	}

	private void initView() {
		ListView lv_left_month = (ListView) view.findViewById(R.id.lv_left_month);
		lv_left_month.setAdapter(new LeftMonthAdapter(activity));

		ListView lv_statis = (ListView) view.findViewById(R.id.lv_statis);
		cursor = application.database.query(SaintDataBaseHelper.TABLE_NAME_MONTH_ITEM, null, null, null, null, null, "orderBy ASC");
		
		adapter = new SimpleCursorAdapter(activity, R.layout.monthstatis_item, cursor,
				new String[] { "month", "normalNum", "baptismNum", "morningRiseNum", "prayNum", "gospelerNum",
						"familyMettNum", "rankMeetNum", "sundayMeetNum" },
				new int[] { R.id.tv_month, R.id.tv_normalNum, R.id.tv_baptism, R.id.tv_morningRise, R.id.tv_prayNum,
						R.id.tv_gospeler, R.id.tv_familyMett, R.id.tv_rankMeet, R.id.tv_sundayMeet },
				SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		lv_statis.setAdapter(adapter);

		lv_left_month.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				intent2MonthData(view, position);
			}
		});
		lv_statis.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				intent2MonthData(view, -1);
			}
		});
	}
	public void intent2MonthData(View view, int position){
		TextView tv_month = (TextView) view.findViewById(R.id.tv_month);
		Intent intent = new Intent(activity, MonthDataActivity.class);
		intent.putExtra("month", tv_month.getText().toString());
		intent.putExtra(Constans.CLICK_POSITION, position);
		startActivityForResult(intent, REQUEST_CODE_MONTH);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_CANCELED) {
			return;
		}
		switch (requestCode) {
		case REQUEST_CODE_MONTH:
			cursor = application.database.query(SaintDataBaseHelper.TABLE_NAME_MONTH_ITEM, null, null, null, null, null, "orderBy ASC");
			adapter.changeCursor(cursor);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (cursor != null) {
			cursor.close();
		}
	}
}
